﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using umbraco;
using umbraco.NodeFactory;
using umbraco.BusinessLogic;
using umbraco.cms.businesslogic.web;
using System.Xml;

namespace ExtendedWorkFlow.DataTypes
{
  
    public class ListBoxMoveBase : UpdatePanel
    {

        #region Variables

        ListBox _Source;
        ListBox _Destination;
        Button _Add;
        Button _Remove;

        Unit _ButtonWidth = new Unit("60px");

        public event EventHandler AddClicked;
        public event EventHandler RemoveClicked;
                
        #endregion

        #region Properties

        public ListBox Source { get { return this._Source; } }
        public ListBox Destination { get { return this._Destination; } }

        #endregion

        #region Init and Constructor

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            this.EnsureChildControls();
            this.ChildrenAsTriggers = true;            
        }

        #endregion

        #region Child Controls

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            Panel pnlContainer = new Panel();

            Table tblEditor = new Table();
            TableRow rowEditor = new TableRow();
            TableCell celSource = new TableCell();
            TableCell celOptions = new TableCell();
            TableCell celDestination = new TableCell();

            celOptions.Style.Add("text-align", "center");
            celOptions.Width = new Unit("75px");

            this._Source = new ListBox();
            this._Source.Width = new Unit("300px");
            this._Source.Height = new Unit("150px");
            this._Source.SelectionMode = ListSelectionMode.Multiple;

            this._Destination = new ListBox();
            this._Destination.Width = new Unit("300px");
            this._Destination.Height = new Unit("150px");
            this._Destination.SelectionMode = ListSelectionMode.Multiple;

            this._Add = new Button();
            this._Add.ID = "add";
            this._Add.Text = "Add";
            this._Add.Width = _ButtonWidth;

            this._Remove = new Button();
            this._Add.ID = "remove";
            this._Remove.Text = "Remove";
            this._Remove.Width = _ButtonWidth;

            celSource.Controls.Add(this._Source);
            celOptions.Controls.Add(this._Add);
            celOptions.Controls.Add(this._Remove);
            celDestination.Controls.Add(this._Destination);

            rowEditor.Cells.Add(celSource);
            rowEditor.Cells.Add(celOptions);
            rowEditor.Cells.Add(celDestination);

            tblEditor.Rows.Add(rowEditor);
            pnlContainer.Controls.Add(tblEditor);

            this.ContentTemplateContainer.Controls.Add(pnlContainer);
            
            this._Add.Click += new EventHandler(Add_Click);
            this._Remove.Click += new EventHandler(Remove_Click);

            this.ChildControlsCreated();
        }

        /// <summary>
        /// Child controls created override
        /// </summary>
        protected virtual new void ChildControlsCreated()
        {
        }

        #endregion

        #region Button Postbacks

        void Remove_Click(object sender, EventArgs e)
        {
            if (this.RemoveClicked != null) RemoveClicked(this, new EventArgs());
        }

        void Add_Click(object sender, EventArgs e)
        {
            if (this.AddClicked != null) AddClicked(this, new EventArgs());
        }

        #endregion

    }

}